package com.stylefeng.guns.rest.domain;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.stylefeng.guns.core.util.Convert;
import com.stylefeng.guns.rest.common.exception.BizExceptionEnum;
import com.stylefeng.guns.rest.common.exception.ParamException;
import com.stylefeng.guns.rest.common.util.StringUtils;

/**
 * 接收分页入参,封装结果对象
 * @author Cailin
 *
 */
public class PageMap extends HashMap<String, Object> {
	/*
	private Integer pageNum;
	private Integer pageSize;//
	private String orderBy;//eg : "sales_num desc"
	private Integer pages;
	private Long total;
	private List list;
	*/
	
	public PageMap() {
		super();
	}
	public PageMap(int pageNum, int pageSize) {
		super();
		setPageNum(pageNum);
		setPageSize(pageSize);
	}
	public PageMap(long total, List list) {
		super();
		setTotal(total);
		setList(list);
	}
	public PageMap(List list) {
		super();
		data(list);
	}
	public PageMap data(List list) {
		setList(list);
		if (list instanceof Page) {
			Page page = (Page) list;
			setTotal(page.getTotal());
			setPages(page.getPages());
			setPageNum(page.getPageNum());
			setPageSize(page.getPageSize());
		}
		return this;
	}

	public PageMap dataEx(List list,Map<String, Object>  map){
		setList(list);
		for(Entry<String, Object> entry:map.entrySet()){
			setStr(entry.getKey(),entry.getValue());
		}
		if (list instanceof Page) {
			Page page = (Page) list;
			setTotal(page.getTotal());
			setPages(page.getPages());
			setPageNum(page.getPageNum());
			setPageSize(page.getPageSize());
		}
		return this;
	}

	public void setStr(String key,Object value) {
		put(key, value);
	}

	public Integer getPageNum() {
		return (Integer) get("pageNum");
	}
	public void setPageNum(Integer pageNum) {
		put("pageNum", pageNum);
	}
	public Integer getPageSize() {
		return (Integer) get("pageSize");
	}
	public void setPageSize(Integer pageSize) {
		put("pageSize", pageSize);
	}
	public Long getTotal() {
		return (Long) get("total");
	}
	public void setTotal(Long total) {
		put("total", total);
	}
	public Integer getPages() {
		return (Integer) get("pages");
	}
	public void setPages(Integer pages) {
		put("pages", pages);
	}
	public List getList() {
		return (List) get("list");
	}
	public void setList(List list) {
		put("list", list);
	}
	public String getOrderBy() {
		return (String) get("orderBy");
	}
	public String getOrderByWith(String lastOrderBy) {
		String orderBy = (String) get("orderBy");
		if(orderBy != null){
			return orderBy + "," + lastOrderBy;
		}else{
			return lastOrderBy;
		}
	}
	public void setOrderBy(String orderBy) {
		put("orderBy", orderBy);
	}
	
	
	/********/
	public String getStrMisValid(String attr) {
		String res = Convert.toStr(get(attr), null);
		if(StringUtils.isBlank(res)){
			throw new ParamException(BizExceptionEnum.ISV_MISSING,attr);
		}
		return res;
	}

	public Integer getIntMisValid(String attr) {
		Integer res = Convert.toInt(get(attr), null);
		if(res == null){
			throw new ParamException(BizExceptionEnum.ISV_MISSING,attr);
		}
		return res;
	}

	@JsonIgnore
	public String getStr(String key){
		return (String) get(key);
	}
	@JsonIgnore
	public Integer getInt(String key){
		try {
			return Integer.valueOf(getStr(key));
		} catch (Exception e) {
			return null;
		}
	}
	@JsonIgnore
	public Long getLong(String key){
		try {
			return Long.valueOf(getStr(key));
		} catch (Exception e) {
			return null;
		}
	}
	@JsonIgnore
	public Double getDouble(String key){
		try {
			return Double.valueOf(getStr(key));
		} catch (Exception e) {
			return null;
		}
	}
	@JsonIgnore
	public Boolean getBoolean(String key){
		try {
			String val = getStr(key);
			//return Boolean.valueOf(getStr(key));
			return ((val != null) && ("true".equalsIgnoreCase(val) || "1".equals(val)));
		} catch (Exception e) {
			return null;
		}
	}
	
	
}
